Page({
  data: {
    userId: null,
    userInfo: {},
    activeTab: 'recipes',
    recipeList: [],
    workList: [],
    isFollowed: false,
    loading: true
  },

  onLoad(options) {
    const { id } = options;
    this.setData({
      userId: id
    });
    
    this.loadUserDetail();
    this.loadUserContent();
  },

  // 加载用户详情
  loadUserDetail() {
    // 模拟API请求
    setTimeout(() => {
      const mockUserInfo = this.generateMockUserInfo(this.data.userId);
      this.setData({
        userInfo: mockUserInfo,
        isFollowed: mockUserInfo.isFollowed || false,
        loading: false
      });
    }, 500);
  },

  // 生成模拟用户信息
  generateMockUserInfo(userId) {
    const users = {
      1: {
        id: 1,
        name: '西瓜',
        avatar: 'https://picsum.photos/120/120?random=1',
        gender: '女',
        joinYear: '2021',
        points: 1280,
        following: 10,
        followers: 200,
        likesReceived: 4,
        recipeCount: 4,
        workCount: 3,
        isVip: true,
        vipExpireDate: '2023/07/20',
        description: '热爱美食的烘焙达人，喜欢分享各种美味的菜谱和作品。',
        isFollowed: false
      },
      2: {
        id: 2,
        name: '美食达人',
        avatar: 'https://picsum.photos/120/120?random=2',
        gender: '男',
        joinYear: '2020',
        points: 2560,
        following: 25,
        followers: 450,
        likesReceived: 12,
        recipeCount: 8,
        workCount: 6,
        isVip: true,
        vipExpireDate: '2024/01/15',
        description: '专业厨师，擅长各种菜系，希望和大家分享美食的快乐。',
        isFollowed: true
      },
      3: {
        id: 3,
        name: '厨房小能手',
        avatar: 'https://picsum.photos/120/120?random=3',
        gender: '女',
        joinYear: '2022',
        points: 890,
        following: 15,
        followers: 120,
        likesReceived: 8,
        recipeCount: 3,
        workCount: 5,
        isVip: false,
        description: '新手烘焙爱好者，正在学习中，请多指教！',
        isFollowed: false
      },
      4: {
        id: 4,
        name: '健康生活家',
        avatar: 'https://picsum.photos/120/120?random=4',
        gender: '女',
        joinYear: '2021',
        points: 1560,
        following: 8,
        followers: 180,
        likesReceived: 6,
        recipeCount: 5,
        workCount: 4,
        isVip: false,
        description: '专注于健康饮食，分享营养均衡的菜谱。',
        isFollowed: false
      },
      5: {
        id: 5,
        name: '甜点大师',
        avatar: 'https://picsum.photos/120/120?random=5',
        gender: '男',
        joinYear: '2019',
        points: 3200,
        following: 30,
        followers: 600,
        likesReceived: 15,
        recipeCount: 12,
        workCount: 8,
        isVip: true,
        vipExpireDate: '2024/03/15',
        description: '专业甜点师，擅长各种西式甜点的制作。',
        isFollowed: false
      },
      6: {
        id: 6,
        name: '营养师小李',
        avatar: 'https://picsum.photos/120/120?random=6',
        gender: '女',
        joinYear: '2020',
        points: 2100,
        following: 12,
        followers: 280,
        likesReceived: 9,
        recipeCount: 6,
        workCount: 7,
        isVip: false,
        description: '专业营养师，提供健康饮食建议和菜谱。',
        isFollowed: false
      }
    };

    return users[userId] || users[1];
  },

  // 加载用户内容
  loadUserContent() {
    // 模拟菜谱数据
    const mockRecipes = [
      {
        id: 1,
        title: '轻食减脂谷物饭',
        imageUrl: 'https://picsum.photos/300/300?random=10',
        isVideo: true,
        likes: 1234
      },
      {
        id: 2,
        title: '黑森林巧克力蛋糕',
        imageUrl: 'https://picsum.photos/300/300?random=11',
        isVideo: false,
        likes: 2345
      },
      {
        id: 3,
        title: '果沙拉',
        imageUrl: 'https://picsum.photos/300/300?random=12',
        isVideo: false,
        likes: 856
      },
      {
        id: 4,
        title: '燕麦香蕉谷物碗',
        imageUrl: 'https://picsum.photos/300/300?random=13',
        isVideo: false,
        likes: 567
      }
    ];

    // 模拟作品数据
    const mockWorks = [
      {
        id: 101,
        title: '巧克力慕斯蛋糕',
        imageUrl: 'https://picsum.photos/300/300?random=20',
        isVideo: false,
        likes: 890
      },
      {
        id: 102,
        title: '蜂蜜蓝莓松饼',
        imageUrl: 'https://picsum.photos/300/300?random=21',
        isVideo: false,
        likes: 654
      },
      {
        id: 103,
        title: '红莓裸蛋糕',
        imageUrl: 'https://picsum.photos/300/300?random=22',
        isVideo: false,
        likes: 432
      }
    ];

    this.setData({
      recipeList: mockRecipes,
      workList: mockWorks
    });
  },

  // 标签切换
  onTabChange(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
  },

  // 关注/取消关注
  onFollowTap() {
    const isFollowed = !this.data.isFollowed;
    const userInfo = { ...this.data.userInfo };
    
    if (isFollowed) {
      userInfo.followers += 1;
    } else {
      userInfo.followers -= 1;
    }
    
    this.setData({
      isFollowed: isFollowed,
      userInfo: userInfo
    });
    
    wx.showToast({
      title: isFollowed ? '关注成功' : '已取消关注',
      icon: 'success'
    });
  },

  // 菜谱点击
  onRecipeTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/index/pages/detail/detail?id=${id}&type=recipe&contentType=1`
    });
  },

  // 作品点击
  onWorkTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/index/pages/detail/detail?id=${id}&type=work&contentType=2`
    });
  },

  // 分享
  onShareTap() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

  // 分享给朋友
  onShareAppMessage() {
    return {
      title: `${this.data.userInfo.name}的个人主页`,
      path: `/index/pages/user-detail/user-detail?id=${this.data.userId}`,
      imageUrl: this.data.userInfo.avatar
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: `${this.data.userInfo.name}的个人主页`
    };
  }
}); 